<?php

namespace View;
require_once ("NavigationView.php");  

	class LoginView {
	  
	  	private $m_username = 'username';
		private $m_password = 'password';
		private $m_loginButton = 'loginButton';
		private $m_logoutButton = 'logoutButton';
		private $m_checkButton = 'checkButton';
		
		
		
		  //Funktion som returnerar HTML för ett inloggningsformulär
		  public function DoLoginBox() {
		    return 
		    '  <div id=text>
          <h3>Share your favorite (or least favorite) movies with your friends, 
          and get movie tips updated as they are sharing theirs. </h3>
          </div>
		    <div id=login>
		    <form method="post">
			    <fieldset>
					Username: <input type="text" name="'.$this->m_username.'" />
					Password: <input type="password" name="'.$this->m_password.'" />
					<input type="submit" value="Login" class="login" name="'.$this->m_loginButton.'"/>
					Remember me<input type="checkbox" value="Remember me" name="'.$this->m_checkButton.'"/>
				</fieldset>
			</form>
			</div>';
		  }
		  
		  public function DoLogoutBox() {
		  	return 
		  	'<div id=logout>
			  	<form method="get">
			  		<input type="submit" value="Logout" id="logout"name="'.$this->m_logoutButton.'" />
			  	</form>
		  	</div>';
		  }
		  
		  //Returnerar användarnamnet
		  public function GetUserName() {
		  	$salt = 'mysalt';	
			
			if (isset($_POST[$this->m_username])) {
		  		return $_POST[$this->m_username];
		  	}
			else if (isset($_COOKIE["username"])){
				$username = $_COOKIE["username"];
				$this->decrypt($username, $salt);
				return $decryptedUsername;
			}
		  	
			else {
				return false;
			}
		  }
		  
		  //Returnerar lösenordet
		  public function GetPassword() {
		  	$salt = 'mysalt';	
			
			if (isset($_POST[$this->m_password])) {
		  		return $_POST[$this->m_password];
		  	}
			else if (isset($_COOKIE["password"])){
				$password = $_COOKIE["password"];
				$decryptedPassword = $this->decrypt($password, $salt);
				return $decryptedPassword;
			}
		  	
			else {
				return false;
			}
		  }
		  
		  //Är remember me boxen iklickad returneras true, annars false
		  public function RememberMe(){
		  	if(isset($_POST[$this->m_checkButton])) {
		  		return true;
		  	}
			else{
				return false;
			}
		  	
		  }
		 
		  //Har användaren klickat på login
		  public function TriedToLogin() {
		  	if (isset($_POST[$this->m_loginButton])) {
		  		if($this->RememberMe()){
					$this->SetCookie($this->GetUserName(), $this->GetPassword());
				}
				return true;
		  	}
			else {
				return false;
			}
		  }
		  
		  //Har användaren klickat på logout
		  public function TriedToLogout() {
		  	
		  	if (isset($_GET[$this->m_logoutButton])) {
		  		return true;
		  	}
			else {
				return false;
			}
		  }
		  
		  //Sätter en coookie
		  public function SetCookie($username, $password) {
		  		$salt = 'mysalt';	
		  		$encryptedUsername=$this->encrypt($username, $salt);
				$encryptedPassword=$this->encrypt($password, $salt);
				
                setcookie("username", $encryptedUsername, time()+3600);
				setcookie("password", $encryptedPassword, time()+3600);
		  }
		
		  //Förstör cookie
		  public function UnsetCookie(){
		  
		  	setcookie("username", "", time()-3600);
			setcookie("password", "", time()-3600);
		  }
		  
		  public function encrypt($text, $salt) { 
			    return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $salt, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)))); 
		  } 

		  public function decrypt($text, $salt) { 
			    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $salt, base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))); 
		  }
		  
		  //Meddelanden till användaren
		  public function SuccessMessage(){
			  $message = "You are now logged in!";
			  return $message;
		  }
		
		  public function WrongUsernameMessage(){
				$message = "<p class='message'>Invalid username or password</p>";
				return $message;
		  }
		  
		  public function LoggedOutMessage(){
				$message = "You are now logged out";
				return $message;
		  }
		  
		 
	}
